import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Determinant extends JPanel implements ActionListener {
	JLabel xpea, ypea, zpea, error, tyhi;
	JTextField x1, y1, z1, x2, y2, z2, x3, y3, z3, vastus;
	JButton arvuta;
	JRadioButton valik, valik1, valik2;
	boolean option = false;
	boolean iid = false;

	public Determinant() {
		setLayout(new GridLayout(6, 3));

		// Layouti esimene rida, P�ISLEIBELID
		xpea = new JLabel(" x ", JLabel.CENTER);
		add(xpea);
		ypea = new JLabel(" y ", JLabel.CENTER);
		add(ypea);
		zpea = new JLabel(" z ", JLabel.CENTER);
		add(zpea);
		zpea.setVisible(false);

		// TEINE RIDA, Tekstiv�ljad
		x1 = new JTextField(5);
		add(x1);
		y1 = new JTextField(5);
		add(y1);
		z1 = new JTextField(5);
		add(z1);
		z1.setVisible(false);

		// KOLMAS RIDA, Tekstiv�ljad
		x2 = new JTextField(5);
		add(x2);
		y2 = new JTextField(5);
		add(y2);
		z2 = new JTextField(5);
		add(z2);
		z2.setVisible(false);

		// NELJAS RIDA, Tekstiv�ljad
		x3 = new JTextField(5);
		add(x3);
		x3.setVisible(false);
		y3 = new JTextField(5);
		add(y3);
		y3.setVisible(false);
		z3 = new JTextField(5);
		add(z3);
		z3.setVisible(false);

		// VIIES RIDA, arvuta error vastus
		arvuta = new JButton("Arvuta");
		arvuta.addActionListener(this);
		add(arvuta);
		vastus = new JTextField(" ");
		add(vastus);
		error = new JLabel("");
		add(error);

		// KUUES RIDA, valik, valik1 , tyhi
		valik = new JRadioButton("Kolmandat järku ruutmaatriks muutujatega");
		valik.addActionListener(this);
		add(valik);
		valik1 = new JRadioButton("Kolmandat järku ruutmaatriks");
		valik1.addActionListener(this);
		add(valik1);
		valik2 = new JRadioButton("Teist järku ruutmaatriks");
		valik2.addActionListener(this);
		add(valik2);
		valik2.setSelected(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Arvuta") {
			error.setText("");

			double x1i = 0, y1i = 0, z1i = 0, x2i = 0, y2i = 0, z2i = 0, x3i = 0, y3i = 0, z3i = 0;
			boolean mati = false;
			try {
				x1i = Double.valueOf(x1.getText());
				y1i = Double.valueOf(y1.getText());
				x2i = Double.valueOf(x2.getText());
				y2i = Double.valueOf(y2.getText());
				if (iid == true) {
					z1i = Double.valueOf(z1.getText());
					z2i = Double.valueOf(z2.getText());
				}
				if (option == true && iid == true) {
					x3i = Double.valueOf(x3.getText());
					y3i = Double.valueOf(y3.getText());
					z3i = Double.valueOf(z3.getText());
				}
				mati = true;
			} catch (NumberFormatException evt) {
				vastus.setText(" ");
				error.setText("Lahtrites peavad olema numbrid");
			}
			if (mati == true) {
				arvutame(x1i, y1i, z1i, x2i, y2i, z2i, x3i, y3i, z3i);
			}

		}
		if (e.getActionCommand() == "Kolmandat järku ruutmaatriks muutujatega") {
			iid = true;
			option = false;
			valik2.setSelected(false);
			valik1.setSelected(false);
			x3.setVisible(false);
			y3.setVisible(false);
			z3.setVisible(false);
			z2.setVisible(true);
			z1.setVisible(true);
			zpea.setVisible(true);

		}
		if (e.getActionCommand() == "Kolmandat järku ruutmaatriks") {
			iid = true;
			option = true;
			valik.setSelected(false);
			valik2.setSelected(false);
			x3.setVisible(true);
			y3.setVisible(true);
			z3.setVisible(true);
			z1.setVisible(true);
			z2.setVisible(true);
			zpea.setVisible(true);

		}
		if (e.getActionCommand() == "Teist järku ruutmaatriks") {
			iid = false;
			option = false;
			valik.setSelected(false);
			valik1.setSelected(false);
			x3.setVisible(false);
			y3.setVisible(false);
			z3.setVisible(false);
			z1.setVisible(false);
			z2.setVisible(false);
			zpea.setVisible(false);
		}
	}

	public void arvutame(double x1i, double y1i, double z1i, double x2i,
			double y2i, double z2i, double x3i, double y3i, double z3i) {
		if (option == false) {
			if (iid == true) {
				String i = ((y1i * z2i) - (y2i * z1i)) + "i";
				double jj = z1i * x2i - x1i * z2i;
				String j = jj + "j";
				if (jj >= 0) {
					j = "+" + jj + "j";
				}
				double kk = x1i * y2i - x2i * y1i;
				String k = kk + "k";
				if (kk >= 0) {
					k = "+" + kk + "k";
				}
				vastus.setText(i + " " + j + " " + k);
			} else {
				vastus.setText(x1i * y2i - x2i * y1i + "");
			}
		} else {
			double answer = x1i * y2i * z3i + y1i * z2i * x3i + x2i * y3i * z1i
					- x3i * y2i * z1i - x2i * y1i * z3i - y3i * z2i * x1i;
			vastus.setText(answer + "");
		}

	}

}
